import DataStore from '../base/DataStore';
import Sprite from '../base/Sprite'
import RoleAction from '../../../Enum/roleAction'
import Handle from '../base/Handle'

/**
    角色基类, 
        控制位置
        不负责绘制图形

        有限状态机
*/

const instructionMap = {
    // 快跑 →↑→
    'forward: keydown,forward: keyup,forward: keydown,': 'run',
    // 快跑+跳 →↑→↑
    'jump: keydown,forward: keydown,forward: keyup,forward: keydown,': 'bigJump',
}

class Role extends Sprite {
    
    // 角色状态
    #status = RoleAction[0]
    #xAxis = 0
    // #yAxis = 0

    constructor(props) {
        super(props)
        this.dataStore = DataStore.getInstance();
        this.ctx = this.dataStore.ctx;
        this.canvas = this.dataStore.canvas;
        this.gameConf = this.dataStore.get('gameConf');
        this.roleConf = this.dataStore.get('roleConf');
        this.handle = new Handle(props.keybord);
        this.instructionMap = {
            ...instructionMap,
            ...props.instructionMap
        }
        // 是否失去控制
        this.localY = false;        // Y轴
        this.localX = false;        // X轴
        this.localSprite = false;   // 角色状态
        // 坠落的时间
        this.time = 0;
        // 动作组
        this.actions = [];
        this.index = 0; // 图片索引

        // 角色状态
        this.direction = props.direction;     // 方向

        // 初始化角色主体
        this.width = 74;
        this.height = 100;
        this.bodyCenter = this.width / 2;
        this.y = 0;
        // this.x = 0;
        this.offsetUp = 0;
        this.jumpHeight = 35;
        this.jumpMaxHiehgt = 55;
    }
    // 角色状态
    get state() {
        return this.#status;
    }
    // 角色状态
    set state(v) {
        if (this.localSprite === false) {
            this.#status = v;
        }
    }
    // 角色 x 轴
    get x() {
        return this.#xAxis;
    }
    set x(v) {
        if (this.localX === false) {
            this.#xAxis = v;
        }
    }
    // 解锁
    unLock(scope) {
        this.lock(scope, false);
    }
    // 上锁
    lock(scope = ['X', 'Y', 'STATUS'], v = true) {
        for (const s of  scope) {
            if (s.toUpperCase() === 'X') {
                this.localX = v;
            }
            if (s.toUpperCase() === 'Y') {
                this.localY = v;
            }
            if (s.toUpperCase() === 'STATUS') {
                this.localSprite = v;
            }
        }
    }
    // 后退
    backward() {
        this.state = RoleAction[4];
        this.move(-1);
    }
    // 前进
    forward () {
        this.state = RoleAction[5];
        this.move(1);
    }
    // 前后移动
    move(num) {
        if (num > 0) {
            this.x += this.gameConf.moveSpeed * 1;
        } else {
            this.x -= this.gameConf.moveSpeed * 1;
        }
    }
    // 静止
    stand() {
        this.state = RoleAction[0];
    }
    // 快跑
    run() {
        this.state = RoleAction[3];
        this.x += this.gameConf.moveSpeed * 2;
    }
    // 跳跃
    jump(num = 30) {
        if (!this.localSprite) {
            if (this.state !== RoleAction[6]) {
                this.state = RoleAction[6];
            }
            // 失控
            if (this.localY === false) {
                this.offsetUp = num;
                this.index = 0; // 图片索引
                this.lock(['y', 'status'], true);
            }
        }
    }
    // 轻拳
    btnA() {
        if (this.state !== RoleAction[7] && this.localSprite === false && !this.isbtnADown) {
            this.state = RoleAction[7];
            this.isbtnADown = true
            // 失控
            this.index = 0; // 图片索引
            console.log('加锁')
            this.lock(['x', 'status'], true);
        }
    }
    un_btnA() {
        this.isbtnADown = false
    }
    // 轻脚
    btnB() {
        if (this.state !== RoleAction[9] && this.localSprite === false && !this.isbtnBDown) {
            this.state = RoleAction[9];
            this.isbtnBDown = true;
            // 失控
            this.index = 0;
            this.lock(['x', 'status'], true);
        }
    }
    un_btnB() {
        this.isbtnBDown = false
    }
    // 轻脚
    btnC() {
        if (this.state !== RoleAction[8] && this.localSprite === false && !this.isbtnCDown) {
            this.isbtnCDown = true
            this.state = RoleAction[8];
            // 失控
            this.index = 0;
            this.lock(['x', 'status'], true);
        }
    }
    un_btnC() {
        this.isbtnCDown = false
    }
    // 重脚
    btnD() {
        if (this.state !== RoleAction[10] && this.localSprite === false && !this.isbtnDDown) {
            this.isbtnDDown = true;
            this.state = RoleAction[10];
            // 失控
            this.index = 0; // 图片索引
            this.lock(['x', 'status'], true);
        }
    }
    un_btnD() {
        this.isbtnDDown = false
    }
    // 快跑时的大跳
    bigJump() {
        this.jump(37);
        this.handle.reset();
    }
    gravitYformula(offsetUp) {
        return (this.time * (this.time - offsetUp)) / 2;
    }

    // 基础绘制
    draw() {
        let staticRole = this.roleConf[this.roleKey][this.state];
        if (this.index > staticRole.length - 1) {
            let once = [
                RoleAction[7],
                RoleAction[8],
                RoleAction[9],
                RoleAction[10],
            ]
            if (once.indexOf(this.state) !== -1) {
                // 单次播放的动画
                console.log('解锁')
                this.unLock();
                this.stand();
            }
            this.index = 0;
        }
        if (this.localY === true) {
            // 掉落的像素
            this.offsetY = this.gravitYformula(this.offsetUp);
            if (this.state === RoleAction[6]) {
                let countFrame = this.offsetUp + 2;
                let diff = (countFrame / 2) / (staticRole.length - 2)
                if (this.time <= (countFrame / 2)) {
                    this.index = Math.ceil(this.time / diff);
                } else {
                    this.index = (staticRole.length - 1) - Math.floor((this.time - (countFrame / 2)) / diff)
                }
            }
            this.y = this.offsetY;
            this.time++;
            if (this.y > 0) {
                this.time = 0;
                this.offsetUp = 0;
                this.y = 0;
                this.unLock();
            }
        }
        if (this.localY !== true) {
            this.index += this.gameConf.spriteSpeed;
        }
    }
    // 根据手柄的动作, 推算出真正意图
    /*
        1. 下蹲
        2. 挑起攻击
        3. 讲解 大招的过程
    */
    instruction() {
        this.actions = [];
        let action = '';
        action = this.handle.KeyStackToString(3);
        console.log(
            action
        )
        let tempComboMap = {
            ...this.instructionMap,
        }
        // 快跑 →→ 3键
        if (action && tempComboMap[action]) {
            this.actions = [tempComboMap[action]];
            return this.actions;
        }
        // 手柄组合/单一 动作
        for (const [key, action] of this.handle.keybordMap) {
            if (action.type === 'keydown') {
                this.actions.push(key);
            } else if (action.type === 'keyup') {
                if (this[`un_${key}`] instanceof Function) {
                    this[`un_${key}`]();
                }
            }
        }
        if (this.actions.length === 0) {
            this.actions.push('stand');
        }
        return this.actions;
    }
    show() {
        // 获取动作组，并执行
        let fnNames = this.instruction();
        for (let fnName of fnNames) {
            if (this[fnName] instanceof Function) {
                this[fnName]();
            }
        }
        // 绘制
        this.draw();
    }
}

export default Role;
